home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.11 Nov 93 / Getting Started / ColorMondrian.c next >
Encoding:
C/C++ Source or Header  |  1994-11-06  |  8.2 KB  |  446 lines  |  [TEXT/KAHL]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  Mondrian Code from Getting Started Column            */
  4. /*    Copyright 1993, Dave Mark, All Rights Reserved       */
  5. /*                                                        */
  6. /********************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <GestaltEqu.h>
  10.  
  11.  
  12. #define kMBARResID            128
  13. #define kErrorAlertID        128
  14. #define kAboutALRTid        129
  15.  
  16. #define kSleep                0L
  17.  
  18. #define kAutoStorage        NULL
  19. #define    kVisible            true
  20. #define kWindowTitle        "\pColor Mondrian"
  21. #define    kMoveToFront        (WindowPtr)-1
  22. #define kNoGoAway            false
  23. #define kNULLRefCon            60L
  24.  
  25. #define mApple                128
  26. #define iAbout                1
  27.  
  28. #define mFile                129
  29. #define iQuit                1
  30.  
  31. #define mDevice                131
  32.  
  33. #define kWindowMargin        5
  34.  
  35. #define kRandomUpperLimit    32768
  36.  
  37. #define kEmptyString        "\p"
  38. #define kNULLFilterProc        NULL
  39.  
  40.  
  41. /*************/
  42. /*  Globals  */
  43. /*************/
  44.  
  45. Boolean        gDone;
  46.  
  47.  
  48. /***************/
  49. /*  Functions  */
  50. /***************/
  51.  
  52. void        ToolboxInit( void );
  53. void        MenuBarInit( void );
  54. void        CreateWindow( GDHandle device );
  55. void        EventLoop( void );
  56. void        DoEvent( EventRecord *eventPtr );
  57. void        HandleMouseDown( EventRecord *eventPtr );
  58. void        HandleMenuChoice( long menuChoice );
  59. void        HandleAppleChoice( short item );
  60. void        HandleFileChoice( short item );
  61. void        HandleDeviceChoice( short item );
  62. Boolean        HasColorQD( void );
  63. GDHandle    GetDeepestDevice( void );
  64. short        GetDeviceDepth( GDHandle device );
  65. void        DrawRandomRect( void );
  66. void        RandomColor( RGBColor *colorPtr );
  67. void        RandomRect( Rect *rectPtr );
  68. short        Randomize( short range );
  69. void        DoError( Str255 errorString );
  70.  
  71.  
  72. /****************** main ***************************/
  73.  
  74. void    main( void )
  75. {
  76.     ToolboxInit();
  77.     
  78.     if ( ! HasColorQD() )
  79.         DoError( "\pThis machine doesn't support Color Quickdraw!" );
  80.     
  81.     MenuBarInit();
  82.     
  83.     CreateWindow( GetDeepestDevice() );
  84.     
  85.     EventLoop();
  86. }
  87.  
  88.  
  89. /****************** ToolboxInit *********************/
  90.  
  91. void    ToolboxInit( void )
  92. {
  93.     InitGraf( &thePort );
  94.     InitFonts();
  95.     InitWindows();
  96.     InitMenus();
  97.     TEInit();
  98.     InitDialogs( nil );
  99.     InitCursor();
  100. }
  101.  
  102.  
  103. /****************** MenuBarInit ***********************/
  104.  
  105. void    MenuBarInit( void )
  106. {
  107.     Handle            menuBar;
  108.     MenuHandle        menu;
  109.     GDHandle        device, deepestDevice;
  110.     Str255            itemStr;
  111.     short            curDeviceNumber = 1;
  112.     
  113.     menuBar = GetNewMBar( kMBARResID );
  114.     SetMenuBar( menuBar );
  115.  
  116.     menu = GetMHandle( mApple );
  117.     AddResMenu( menu, 'DRVR' );
  118.     
  119.     menu = GetMHandle( mDevice );
  120.     
  121.     deepestDevice = GetDeepestDevice();
  122.     
  123.     device = GetDeviceList();
  124.     
  125.     while ( device != NULL )
  126.     {
  127.         itemStr[0] = 10;
  128.         sprintf( (char *)(&(itemStr[1])), "0x%08lX", (unsigned long)device );
  129.         AppendMenu( menu, itemStr );
  130.         
  131.         if ( device == deepestDevice )
  132.             CheckItem( menu, curDeviceNumber, true );
  133.         
  134.         device = GetNextDevice( device );
  135.         curDeviceNumber++;
  136.     }
  137.     
  138.     DrawMenuBar();
  139. }
  140.  
  141.  
  142. /****************** CreateWindow ***********************/
  143.  
  144. void    CreateWindow( GDHandle device )
  145. {
  146.     WindowPtr    window;
  147.     Rect        wBounds;
  148.     
  149.     wBounds = (**device).gdRect;
  150.     
  151.     if ( device == GetMainDevice() )
  152.         wBounds.top += GetMBarHeight();
  153.         
  154.     InsetRect( &wBounds, kWindowMargin, kWindowMargin );
  155.     
  156.     window = NewCWindow( kAutoStorage,
  157.                         &wBounds,
  158.                         kWindowTitle,
  159.                         kVisible,
  160.                         altDBoxProc,
  161.                         kMoveToFront,
  162.                         kNoGoAway,
  163.                         kNULLRefCon );
  164.     
  165.     if ( window == nil )
  166.     {
  167.         DoError( "\pCouldn't create window!" );
  168.     }
  169.     else
  170.     {
  171.         ShowWindow( window );
  172.         SetPort( window );
  173.     }
  174. }
  175.  
  176.  
  177. /******************************** EventLoop *********/
  178.  
  179. void    EventLoop( void )
  180. {        
  181.     EventRecord        event;
  182.     
  183.     GetDateTime( (unsigned long *)(&randSeed) );
  184.     
  185.     gDone = false;
  186.     while ( gDone == false )
  187.     {
  188.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  189.             DoEvent( &event );
  190.         
  191.         DrawRandomRect();
  192.     }
  193. }
  194.  
  195.  
  196. /************************************* DoEvent     */
  197.  
  198. void    DoEvent( EventRecord *eventPtr )
  199. {
  200.     char    theChar;
  201.     
  202.     switch ( eventPtr->what )
  203.     {
  204.         case mouseDown: 
  205.             HandleMouseDown( eventPtr );
  206.             break;
  207.         case keyDown:
  208.         case autoKey:
  209.             theChar = eventPtr->message & charCodeMask;
  210.             if ( (eventPtr->modifiers & cmdKey) != 0 ) 
  211.                 HandleMenuChoice( MenuKey( theChar ) );
  212.             break;
  213.     }
  214. }
  215.  
  216.  
  217. /************************************* HandleMouseDown */
  218.  
  219. void    HandleMouseDown( EventRecord *eventPtr )
  220. {
  221.     WindowPtr        window;
  222.     short            thePart;
  223.     long            menuChoice;
  224.     
  225.     thePart = FindWindow( eventPtr->where, &window );
  226.     
  227.     switch ( thePart )
  228.     {
  229.         case inMenuBar:
  230.             menuChoice = MenuSelect( eventPtr->where );
  231.             HandleMenuChoice( menuChoice );
  232.             break;
  233.         case inSysWindow : 
  234.             SystemClick( eventPtr, window );
  235.             break;
  236.     }
  237. }
  238.  
  239.  
  240. /****************** HandleMenuChoice ***********************/
  241.  
  242. void    HandleMenuChoice( long menuChoice )
  243. {
  244.     short    menu;
  245.     short    item;
  246.     
  247.     if ( menuChoice != 0 )
  248.     {
  249.         menu = HiWord( menuChoice );
  250.         item = LoWord( menuChoice );
  251.         
  252.         switch ( menu )
  253.         {
  254.             case mApple:
  255.                 HandleAppleChoice( item );
  256.                 break;
  257.             case mFile:
  258.                 HandleFileChoice( item );
  259.                 break;
  260.             case mDevice:
  261.                 HandleDeviceChoice( item );
  262.                 break;
  263.         }
  264.         HiliteMenu( 0 );
  265.     }
  266. }
  267.  
  268.  
  269. /****************** HandleAppleChoice ***********************/
  270.  
  271. void    HandleAppleChoice( short item )
  272. {
  273.     MenuHandle    appleMenu;
  274.     Str255        accName;
  275.     short        accNumber;
  276.     
  277.     switch ( item )
  278.     {
  279.         case iAbout:
  280.             NoteAlert( kAboutALRTid, kNULLFilterProc );
  281.             break;
  282.         default:
  283.             appleMenu = GetMHandle( mApple );
  284.             GetItem( appleMenu, item, accName );
  285.             accNumber = OpenDeskAcc( accName );
  286.             break;
  287.     }
  288. }
  289.  
  290.  
  291. /****************** HandleFileChoice ***********************/
  292.  
  293. void    HandleFileChoice( short item )
  294. {
  295.     switch ( item )
  296.     {
  297.         case iQuit :
  298.             gDone = true;
  299.             break;
  300.     }
  301. }
  302.  
  303.  
  304. /****************** HandleDeviceChoice ***********************/
  305.  
  306. void    HandleDeviceChoice( short item )
  307. {
  308. /*    Try this: Modify the program so that when a device is selected
  309.     from the Device menu, the current window gets closed and a
  310.     new window is opened on the selected device. Be careful when
  311.     you translate the menu item back into an address. Debug your
  312.     program thoroughly before you try to use the address as an
  313.     address. You don't want to accidentally reformat your hard 
  314.     drive, right?
  315.     
  316.     Also, don't forget to update the check mark!
  317. */
  318. }
  319.  
  320.  
  321. /****************** HasColorQD *****************/
  322.  
  323. Boolean    HasColorQD( void )
  324. {
  325.     unsigned char    version[ 4 ];
  326.     OSErr            err;
  327.     
  328.     err = Gestalt( gestaltQuickdrawVersion, (long *)version );
  329.     
  330.     if ( err != noErr )
  331.     {
  332.         SysBeep( 10 );    /*  Error calling Gestalt!!!  */
  333.         ExitToShell();
  334.     }
  335.     
  336.     if ( version[ 2 ] > 0 )
  337.         return( true );
  338.     else
  339.         return( false );
  340. }
  341.  
  342.  
  343. /****************** GetDeepestDevice *****************/
  344.  
  345. GDHandle    GetDeepestDevice( void )
  346. {
  347.     GDHandle    curDevice, maxDevice = NULL;
  348.     short        curDepth, maxDepth = 0;
  349.     
  350.     curDevice = GetDeviceList();
  351.     
  352.     while ( curDevice != NULL )
  353.     {
  354.         curDepth = GetDeviceDepth( curDevice );
  355.         
  356.         if ( curDepth > maxDepth )
  357.         {
  358.             maxDepth = curDepth;
  359.             maxDevice = curDevice;
  360.         }
  361.  
  362.         curDevice = GetNextDevice( curDevice );
  363.     }
  364.     
  365.     return( maxDevice );
  366. }
  367.  
  368.  
  369. /****************** GetDeviceDepth *****************/
  370.  
  371. short    GetDeviceDepth( GDHandle device )
  372. {
  373.     PixMapHandle    screenPixMapH;
  374.     
  375.     screenPixMapH = (**device).gdPMap;
  376.     
  377.     return( (**screenPixMapH).pixelSize );
  378. }
  379.  
  380.  
  381. /****************** DrawRandomRect *****************/
  382.  
  383. void    DrawRandomRect( void )
  384. {
  385.     Rect        randomRect;
  386.     RGBColor    color;
  387.     
  388.     RandomRect( &randomRect );
  389.     RandomColor( &color );
  390.     RGBForeColor( &color );
  391.     PaintOval( &randomRect );
  392. }
  393.  
  394.  
  395. /****************** RandomColor *********************/
  396.  
  397. void    RandomColor( RGBColor *colorPtr )
  398. {
  399.     colorPtr->red = Random() + 32767;
  400.     colorPtr->blue = Random() + 32767;
  401.     colorPtr->green = Random() + 32767;
  402. }
  403.  
  404.  
  405. /****************** RandomRect *********************/
  406.  
  407. void    RandomRect( Rect *rectPtr )
  408. {
  409.     WindowPtr    window;
  410.  
  411.     window = FrontWindow();
  412.     
  413.     rectPtr->left = Randomize( window->portRect.right
  414.         - window->portRect.left );
  415.     rectPtr->right = Randomize( window->portRect.right
  416.         - window->portRect.left );
  417.     rectPtr->top = Randomize( window->portRect.bottom
  418.         - window->portRect.top );
  419.     rectPtr->bottom = Randomize( window->portRect.bottom
  420.         - window->portRect.top );
  421. }
  422.  
  423.  
  424. /****************** Randomize **********************/
  425.  
  426. short    Randomize( short range )
  427. {
  428.     long    randomNumber;
  429.     
  430.     randomNumber = Random();
  431.     
  432.     if ( randomNumber < 0 )
  433.         randomNumber *= -1;
  434.     
  435.     return( (randomNumber * range) / kRandomUpperLimit );
  436. }
  437.  
  438.  
  439. /***************** DoError ********************/
  440.  
  441. void    DoError( Str255 errorString )
  442. {
  443.     ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
  444.     
  445.     StopAlert( kErrorAlertID, kNULLFilterProc );
  446. }